#include<bits/stdc++.h>
using namespace std;
const long long MOD = 1e9+7;
long long P[32];
int main()  {
  int t; cin>>t;
  P[0] = 1;
  while(t--) {
     long long n, k; cin>>n>>k;
     long long ans = 0;
     for(int i=1; i<=31; ++i) {
       P[i] = (P[i-1]*n)%MOD;
     }
     for(int i=0; i<=31; ++i) {
       if(k & (1<<i)) {
         ans = (ans+P[i])%MOD;
       }
     }
     cout<<ans<<'\n';
  }
}